from rest_framework.decorators import action
from rest_framework.generics import ListAPIView, UpdateAPIView
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import UpdateModelMixin
from apps.orders.models import OrderInfo
from apps.meiduo_admin.serializers.order import OrderInfoSerializer
from apps.meiduo_admin.utils import PageNum
from rest_framework.permissions import DjangoModelPermissions

#查询订单信息视图
class OrderInfoListView(ListAPIView):

    permission_classes = [DjangoModelPermissions]
    queryset = OrderInfo.objects.all()
    serializer_class = OrderInfoSerializer
    pagination_class = PageNum


# 获取单个详情数据
class OrderInfoDetailView(GenericAPIView):

    queryset = OrderInfo.objects.all()
    serializer_class = OrderInfoSerializer
    def get(self, request, pk):
        data = self.get_object()
        serializer = self.get_serializer(data)
        return Response(serializer.data)

    def put(self, request, pk):
        data = self.get_object()
        status = request.data.get('status')
        data.status = status
        data.save()
        serializer = self.get_serializer(data)
        return Response({
            'order_id': serializer.data.get('order'),
            'status': status
        })
    # @action(methods=['put'], detail=True)
    # def status(self, request, pk):
    #     order = self.get_object()
    #     status = request.data.get('status')
    #     order.status = status
    #     order.save()
    #
    #     serializer = self.get_serializer(order)
    #     return Response({
    #         'order_id': order.order_id,
    #         'status': status
    #     })
